package com.qf.ssm01.controller;

import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

@Controller
@RequestMapping("download")
public class DownloadController {

    @RequestMapping("test1")
    public void download(String name, HttpSession session, HttpServletResponse response) throws IOException {
        System.out.println("name:"+name);
        //获得要下载文件的绝对路径
        String path = session.getServletContext().getRealPath("/upload_file");
        //文件的完整路径
        String real_path = path+"\\"+name;

        //设置响应头  告知浏览器，要以附件的形式保存内容   filename=浏览器显示的下载文件名
        response.setHeader("content-disposition","attachment;filename="+name);

        //读取目标文件，写出给客户端
        IOUtils.copy(new FileInputStream(real_path), response.getOutputStream());

        //上一步，已经是响应了,所以此handler直接是void
    }

}
